import io.ktor.client.HttpClient
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.plugins.HttpTimeout
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.defaultRequest
import io.ktor.client.plugins.websocket.WebSockets
import io.ktor.http.ContentType
import io.ktor.serialization.kotlinx.KotlinxSerializationConverter
import kotlinx.serialization.json.Json

class NetworkFactory {
    fun createHttpClient(
        apiUrl: String
    ): HttpClient {
        return HttpClient(
            OkHttp
        ) {
            install(HttpTimeout) {
                requestTimeoutMillis = 1000L * 10
            }

            install(ContentNegotiation) {
                register(
                    ContentType.Text.Html,
                    KotlinxSerializationConverter(
                        Json {
                            prettyPrint = true
                            isLenient = true
                            ignoreUnknownKeys = true
                            encodeDefaults = true
                        }
                    )
                )
                register(
                    ContentType.Application.Json,
                    KotlinxSerializationConverter(
                        Json {
                            prettyPrint = true
                            isLenient = true
                            ignoreUnknownKeys = true
                            encodeDefaults = true
                        }
                    )
                )
            }


            defaultRequest {
                url(apiUrl)
            }
        }
    }
}
